<?php

namespace App\Console\Commands;

use App\Models\Hospital;
use App\Models\HospitalTeam;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Team;
use Illuminate\Console\Command;

class migrationHospital extends Command
{
    public static $type = [1=>1,2=>2,0=>3];
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:hospital';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $oldHospital = OldHospital::query()->get();
        foreach($oldHospital as $v){
            $hospital = OldHospitals::query()->where('id',$v->hospitalid)->first();
            if(!$hospital){
                continue;
            }
            $row['id'] = $v->userid;
            $row['name'] = $hospital->name;
            $row['intro'] = $v->intro;
            $row['province'] = $v->province;
            $row['county'] = $v->county;
            $row['city'] = $v->city==11?1100:$v->city;
            $row['independent'] = $v->is_guanfang;
            $row['longitude'] = $v->longitude;
            $row['latitude'] = $v->latitude;
            $row['qrcode'] = $v->qrcode;
            $row['phone'] = $v->phone;
            $row['clinic_time'] = $v->skilful;
//            if(in_array($hospital->county,[1106,1105,1109,1116,1113,1112]) || in_array($hospital->userid,[143296,118080,126118,126122,160226,113896,113478,91722,175877,176156,184741,184793,190922,192821,194606,206259,206262,206260,213579,213390,213581,216592,216593,216594,219333,221895,223413,228039,240074,240185,240188,240189,240191,248035,248033,281082,281083,281085,281087,281091,281092,281097])) {
//                $row['is_family'] = 1;
//            }else{
//                $row['is_family'] = 0;
//            }
            $row['is_family'] = 0;

            $data[] = $row;

        }
        Hospital::query()->upsert($data,['id']);

        $hospitals = Hospital::all();
        $totle = Team::query()->count();
        $totle = $totle + 1000;
        $data = [];
        $row = [];
        foreach ($hospitals as $hospital) {
            $totle++;
            $team = Team::query()->where('doctorid', $hospital->id)->get();
            if($team->isNotEmpty()) {
                foreach ($team as $t) {
                    $row['id'] = $t->id;
                    $row['hospital_id'] = $hospital->id;
                    $row['name'] = $t->title;
                    $row['intro'] = $t->intro;
                    $row['type'] = self::$type[$t->type];
                    HospitalTeam::query()->updateOrCreate(['hospital_id' => $hospital->id, 'name' => $t->title],$row);
                }
            }else{
                $userDoctor = OldHospital::query()->where('userid',$hospital->id)->first();
                $row['id'] = $totle;
                $row['hospital_id'] = $hospital->id;
                $row['name'] = $userDoctor->name;
                $row['intro'] = $userDoctor->intro;
                $row['type'] = 3;
                HospitalTeam::query()->updateOrCreate(['hospital_id' => $hospital->id, 'name' => $userDoctor->name],$row);
            }
        }

        return 0;
    }
}
